package com.business_clean.ui.activity.custom;

import static com.business_clean.app.config.Constant.TYPE_INIT_DATA;
import static com.business_clean.app.config.Constant.TYPE_INIT_LOAD_MORE;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.business_clean.R;
import com.business_clean.app.App;
import com.business_clean.app.base.BaseActivity;
import com.business_clean.app.callback.OnDialogConfirmListener;
import com.business_clean.app.ext.CommonUtils;
import com.business_clean.app.uitl.ActivityForwardUtil;
import com.business_clean.app.uitl.CheckListDataUtil;
import com.business_clean.data.mode.contact.ContactMangerEntity;
import com.business_clean.databinding.ActivityCommonContactManagementBinding;
import com.business_clean.ui.adapter.custom.contact.ContactManagerAdapter;
import com.business_clean.viewmodel.request.ContactViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;

public class CommonContactManagementActivity extends BaseActivity<ContactViewModel, ActivityCommonContactManagementBinding> {


    private int page = 1;
    private int requestType;
    private ContactManagerAdapter mAdapter;

    private String project_uuid;

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        if (getIntent() != null && getIntent().getExtras() != null) {
            project_uuid = getIntent().getExtras().getString("project_uuid");
        }
        mAdapter = new ContactManagerAdapter();
        mDatabind.list.recyclerView.setAdapter(mAdapter);
        mDatabind.list.recyclerView.setLayoutManager(new LinearLayoutManager(this));

        getMDatabind().list.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                requestMore();
            }

            @Override
            public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                requestOne();
            }
        });
        mDatabind.bottom.butBase.setText("+ 添加常用联系人");
        requestOne();
    }


    @Override
    public void initObserver() {
        App.appViewModelInstance.getRefreshContactManagerList().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                requestOne();
            }
        });
    }

    @Override
    public void onBindViewClick() {
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("uuid", mAdapter.getData().get(position).getUuid());
                bundle.putString("jump_type", "2");//1是公司，2是常用联系人
                ActivityForwardUtil.startActivity(AddCompanyActivity.class, bundle);
            }
        });
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter, @NonNull @NotNull View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_item_contact_edit:
                        Bundle bundle = new Bundle();
                        bundle.putString("uuid", mAdapter.getData().get(position).getUuid());
                        bundle.putString("jump_type", "2");//1是公司，2是常用联系人
                        ActivityForwardUtil.startActivity(AddCompanyActivity.class, bundle);
                        break;
                    case R.id.tv_item_contact_del:
                        CommonUtils.showGeneralDialog(CommonContactManagementActivity.this, "删除常用联系人", "是否删除？",
                                "取消", "确定", null, new OnDialogConfirmListener() {
                                    @Override
                                    public void onConfirm() {
                                        HashMap<String, String> hashMap = new HashMap<>();
                                        hashMap.put("uuid", "" + mAdapter.getData().get(position).getUuid());
                                        hashMap.put("project_uuid", "" + App.appViewModelInstance.getUserInfo().getValue().getProject().getUuid());
                                        mViewModel.requestDelContact(hashMap);
                                    }
                                });
                        break;
                }
            }
        });


        mDatabind.bottom.butBase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("jump_type", "2");//2是常用联系人
                ActivityForwardUtil.startActivity(AddCompanyActivity.class, bundle);
            }
        });
    }


    @Override
    public void onRequestSuccess() {
        mViewModel.getContactAllManger().observe(this, new Observer<ContactMangerEntity>() {
            @Override
            public void onChanged(ContactMangerEntity contactMangerEntity) {
                switch (requestType) {
                    case TYPE_INIT_DATA:
                        // 根据数据来决定要显示哪一个状态的View
                        if (CheckListDataUtil.checkInitData(contactMangerEntity.getList(), mAdapter, getMDatabind().list.refreshLayout)) {
                            mAdapter.setList(contactMangerEntity.getList());
                        }
                        break;
                    case TYPE_INIT_LOAD_MORE:
                        // 根据数据来决定要显示哪一个状态的View
                        if (CheckListDataUtil.checkLoadMoreData(contactMangerEntity.getList(), mAdapter, getMDatabind().list.refreshLayout)) {
                            mAdapter.addData(contactMangerEntity.getList());
                        }
                        break;
                }
            }
        });

        mViewModel.getDelContactUpdate().observe(this, new Observer<Object>() {
            @Override
            public void onChanged(Object o) {
                requestOne();
            }
        });
    }


    void requestOne() {
        requestType = TYPE_INIT_DATA;
        page = 1;
        mViewModel.getContactAll(page,project_uuid);
    }

    void requestMore() {
        requestType = TYPE_INIT_LOAD_MORE;
        page++;
        mViewModel.getContactAll(page,project_uuid);
    }
}